﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using GBlason.Culture;
using GBlason.Properties;
using GBlason.ViewModel;
using GBlason.ViewModel.Contract;

namespace GBlason.Common.CustomCommand
{
    public class ChangeColorCommand: CommandGeneric
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeShapeCommand"/> class.
        /// </summary>
        /// <param name="ownertype">The ownertype.</param>
        public ChangeColorCommand(Type ownertype)
            : base("Change color", ownertype)
        {
        }

        private TinctureViewModel _oldColor;

        private TinctureViewModel _newColor;

        
        /// <summary>
        /// Does this instance.
        /// </summary>
        /// <param name="parameter"></param>
        public override void Do(CommandParameter parameter)
        {
            _newColor = parameter.Parameter as TinctureViewModel;

            var objectTargeted = parameter.ObjectTargeted as IFillable;

            if (_newColor == null || objectTargeted == null)
                throw new ArgumentNullException("parameter");

            _oldColor = objectTargeted.ColorFill;

            objectTargeted.ColorFill = _newColor;

            Description = String.Format(CultureInfo.CurrentCulture,
                                        Resources.CommandDescChangeColor,
                                        _oldColor == null ? BlasonVocabulary.TinctureNone : _oldColor.LocalizedName,
                                        _newColor.LocalizedName,
                                        objectTargeted is CoatOfArmComponent ? ((CoatOfArmComponent)objectTargeted).ComponentName : String.Empty);

            //sauvegarde, c'est affreux, je peux pas le faire dans la classe abstraite parceque c'est cette instance que je dois cloner :(
            //copie de code pour tous les execute !!
            Parameter = parameter;
            Done = true;
            parameter.FileTargeted.CommandsApplied.AddCommandHistory((ChangeColorCommand)Clone());
        }

        public override void Redo()
        {
            Undo();
            Done = true;
        }

        /// <summary>
        /// Undoes this instance.
        /// Setting the old shape to the coat of arms
        /// </summary>
        public override void Undo()
        {
            var coaTarget = Parameter.ObjectTargeted as IFillable;
            if (coaTarget == null) return;
            coaTarget.ColorFill = _oldColor;
            _oldColor = _newColor;
            _newColor = coaTarget.ColorFill;
            Done = false;
        }

        public override object Clone()
        {
            return MemberwiseClone();
        }
    }
}
